<template>
  <el-dialog title="添加员工" :visible="showDialog" center @close="close">
    <el-form ref="form" label-position="left" :rules="rules" style="padding:0 40px" :model="formData">
      <!-- 姓名 -->
      <el-form-item label="姓名:" label-width="80px" prop="username">
        <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <!-- 手机号 -->
      <el-form-item label="手机号:" label-width="80px" prop="mobile">
        <el-input v-model="formData.mobile" style="width:70%" placeholder="手机号" />
      </el-form-item>
      <!-- 入职时间 -->
      <el-form-item label="入职时间:" label-width="80px">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" style="width: 50%;" value-format="timestamp" />
      </el-form-item>
      <!-- 聘用方式 -->
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:70%" placeholder="请选择">
          <el-option value="1" label="正式" />
          <el-option value="2" label="非正式" />
        </el-select>
      </el-form-item>
      <!-- 工号 -->
      <el-form-item label="工号" label-width="80px" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:70%" placeholder="请输入工号" />
      </el-form-item>
      <!-- 部门 -->
      <el-form-item label="部门" label-width="80px" prop="departmentName" style="position: relative;">
        <el-input v-model="formData.departmentName" style="width:70%" placeholder="请选择部门" @focus="getDepartments" />
        <!-- 放置一个tree组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          default-expand-all=""
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="close">取消</el-button>
          <el-button type="primary" size="small" @click="add">提交</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {

  props: {
    showDialog: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      formData: {
        username: '', // 姓名
        mobile: '', //  手机号
        formOfEmployment: '', // 聘用方式
        workNumber: '', // 工号
        departmentName: '', // 部门
        // inServiceStatus: 1
        timeOfEntry: '' // 入职时间
      },
      //  定义校验规则
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-6位'
        }],
        // mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
        //   pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        // }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },
  methods: {
    //  获取树形结构 选择部门，赋值表单数据
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      // depts是数组 但不是树形
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 点击部门时触发 把点击的 部门 赋值到input框里面
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    close() { // 关闭 添加员工面板 重置表单
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
      this.formData = {
        username: '', // 姓名
        mobile: '', //  手机号
        formOfEmployment: '', // 聘用方式
        workNumber: '', // 工号
        departmentName: '' // 部门
      }
    },
    //  添加
    async add() {
      try {
        await addEmployee({ ...this.formData })
        this.$parent.getEmployeesList() // / 调用父组件的 获取新数据的方法
        this.close()// 关闭添加员工 面板
        this.$message.success('添加成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped>
.el-tree{
  width: 100%;
  position: absolute;
  z-index: 999;
}
</style>
